package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.sell.dao.OrderReviewProcessDao;
import com.wing.sell.model.entity.OrderReviewProcess;
import com.wing.sell.model.entity.OrderReviewProcessCompany;
import com.wing.sell.model.request.OrderReviewProcessForm;
import com.wing.sell.model.response.OrderReviewProcessVO;
import com.wing.sell.service.OrderReviewProcessCompanyService;
import com.wing.sell.service.OrderReviewProcessService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单评审环节-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderReviewProcessServiceImpl extends ServiceImpl<OrderReviewProcessDao, OrderReviewProcess> implements OrderReviewProcessService {

    @Autowired
    private OrderReviewProcessCompanyService orderReviewProcessCompanyService;

    @Override
    @Transactional(readOnly = false)
    public int add(OrderReviewProcessForm orderReviewProcessForm) {

        //校验

        OrderReviewProcess orderReviewProcess =new OrderReviewProcess();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderReviewProcess.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderReviewProcessForm, orderReviewProcess, copyOptions);

        int i = baseMapper.insert(orderReviewProcess);
        if(i > 0){
            for (OrderReviewProcessCompany orderReviewProcessCompany : orderReviewProcessForm.getOrderReviewProcessCompanyList()) {
                orderReviewProcessCompany.setCreator(JwtUtils.getLoginUserId());
                orderReviewProcessCompany.setCreatorName(JwtUtils.getLoginUsername());
                orderReviewProcessCompany.setModifier(JwtUtils.getLoginUserId());
                orderReviewProcessCompany.setModifierName(JwtUtils.getLoginUsername());
                orderReviewProcessCompany.setProcessId(orderReviewProcess.getId());
            }
            orderReviewProcessCompanyService.saveBatch(orderReviewProcessForm.getOrderReviewProcessCompanyList());
        }
        return i;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean update(OrderReviewProcess orderReviewProcess) {
        int i = baseMapper.updateById(orderReviewProcess);
        if(i > 0){
            List<OrderReviewProcessCompany> addCompanys = new ArrayList<>();
            List<OrderReviewProcessCompany> updCompanys = new ArrayList<>();
            List<Long> delCompanys = new ArrayList<>();
            List<Long> ids = new ArrayList<>();
            for (OrderReviewProcessCompany orderReviewProcessCompany : orderReviewProcess.getOrderReviewProcessCompanyList()) {
                if(orderReviewProcessCompany.getId() == null){
                    orderReviewProcessCompany.setCreator(JwtUtils.getLoginUserId());
                    orderReviewProcessCompany.setCreatorName(JwtUtils.getLoginUsername());
                    orderReviewProcessCompany.setModifier(JwtUtils.getLoginUserId());
                    orderReviewProcessCompany.setModifierName(JwtUtils.getLoginUsername());
                    orderReviewProcessCompany.setProcessId(orderReviewProcess.getId());
                    addCompanys.add(orderReviewProcessCompany);
                }else{
                    ids.add(orderReviewProcessCompany.getId());
                    updCompanys.add(orderReviewProcessCompany);
                }
            }
            // 查询旧数据
            List<OrderReviewProcessCompany> oldCompanys = orderReviewProcessCompanyService.list(new QueryWrapper<OrderReviewProcessCompany>().eq("process_id", orderReviewProcess.getId()));
            for (OrderReviewProcessCompany oldCompany : oldCompanys) {
                if(!ids.contains(oldCompany.getId())){
                    delCompanys.add(oldCompany.getId());
                }
            }
            if(addCompanys.size() > 0){
                orderReviewProcessCompanyService.saveBatch(addCompanys);
            }
            if(updCompanys.size() > 0){
                orderReviewProcessCompanyService.updateBatchById(updCompanys);
            }
            if(delCompanys.size() > 0){
                orderReviewProcessCompanyService.removeByIds(delCompanys);
            }
        }
        return i > 0;
    }

    @Override
    public List<OrderReviewProcessVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderReviewProcessVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderReviewProcessVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderReviewProcessVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
